package com.changgou.canal;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.xpand.starter.canal.annotation.*;

@CanalEventListener
public class CanalDataEventListener {
    /***
     * 增加数据监听
     * @param eventType 当前操作的类型



























     @param rowData 发生变更的一行数据
     */
    @InsertListenPoint
    public void onEventInster(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach(c -> System.out.println("增加后的数据：" + c.getName() + "==" + c.getValue()));
    }

    /***
     * 修改数据监听
     * @param rowData
     */
    @UpdateListenPoint
    public void onEventUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getBeforeColumnsList().forEach(c -> System.out.println("修改前的数据：" + c.getName() + "==" + c.getValue()));
        System.out.println("============");
        rowData.getAfterColumnsList().forEach(c -> System.out.println("修改后的数据：" + c.getName() + "==" + c.getValue()));
    }

    /***
     * 删除数据监听
     * @param eventType
     */
    @DeleteListenPoint
    public void onEventDelete(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getBeforeColumnsList().forEach(c -> System.out.println("删除前的数据：" + c.getName() + "==" + c.getValue()));
    }

     /**
     * 自定义数据修改监听
     * @param eventType
     * @param rowData
     */

    @ListenPoint(eventType ={ CanalEntry.EventType.DELETE, CanalEntry.EventType.UPDATE},
    schema = {"changgou_business"},  //库名
    table = {"tb_activity"},         //表名
    destination = "example")         //实例名
    public void onEventCustomUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getBeforeColumnsList().forEach(c -> System.out.println("自定义前的数据：" + c.getName() + "==" + c.getValue()));
        System.out.println("============");
        rowData.getAfterColumnsList().forEach(c -> System.out.println("自定义后的数据：" + c.getName() + "==" + c.getValue()));    }
}

